﻿using LdtDataEngin.ModuleDetail.AnalysisModule.AnalysisDataSave;
using LdtDataEngin.ModuleDetail.CommonModule.XmlCodeCommon;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataSave;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;

namespace LdtDataEngin.Moudle
{
    /// <summary>
    /// 分析数据保存模块
    /// </summary>
    public class MoudleSaveAnalysisData : MoudleBase
    {
        private IMoudle _moudle;
        private static string _moudleResult;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="moudle"></param>
        public MoudleSaveAnalysisData(IMoudle moudle)
        {
            _moudle = moudle;
        }
        /// <summary>
        /// 执行模块
        /// </summary>
        /// <param name="dataContext"></param>
        public override void MoudleHandle(DataContext dataContext)
        {
            //执行当前模块的业务逻辑
            string moudleName = typeof(MoudleSaveAnalysisData).FullName;
            moudleName += "_" + this.GetHashCode();
            // 设置模块进度
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName, 0, dataContext.InteractiveShow.inputParameterModel);
            // 创建计时器
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            try
            {
                var configModel = dataContext.DataLayerSys.GetConfigModel(moudleName);
                XmlDocument xmlDocument = new XmlDocument();

                if (configModel.ConfigDetail != null && configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "caseelementext") != null)
                {
                    xmlDocument.Load(configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "caseelementext").ConfigContent);
                }
                ReorderModel reorderModel = null;
                if (configModel.ConfigDetail != null && configModel.ConfigDetail.FirstOrDefault(a => a.Code == "ordercondition") != null)
                {
                    reorderModel = XmlUtility.DeserializeToObject<ReorderModel>(configModel.ConfigDetail.FirstOrDefault(a => a.Code == "ordercondition").ConfigContent);
                }
                LdtJudge.DataEngine.Common.DataSave.DataResultSave.AnalysisRequests.Clear();

                XmlCodeStartModel xmlCodeStartModel = new XmlCodeStartModel() { dataContext = dataContext };
                if (xmlCodeStartModel.Extract(configModel))
                {
                    AnalysisDataSaveAsseml analysisDataSaveAsseml = new AnalysisDataSaveAsseml(dataContext, moudleName);
                    analysisDataSaveAsseml.DataSave();
                }


                _moudleResult = dataContext.DataLayerAns.AnalysisResultsXml.OuterXml;
            }
            catch (Exception ex)
            {
                dataContext.InteractiveShow.LogShow("数据分析保存模块错误：" + ex.ToString());
            }

            // 设置模块进度
            stopwatch.Stop();

            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName + "_" + stopwatch.Elapsed.TotalMilliseconds.ToString("0"), 100, dataContext.InteractiveShow.inputParameterModel);
            //执行完毕调用下一个模块
            _moudle.MoudleHandle(dataContext);
        }

        /// <summary>
        /// 模块处理结果
        /// </summary>
        /// <returns></returns>
        public static HandleResultModel MoudleHandleResult()
        {
            HandleResultModel resultModel = new HandleResultModel();
            if (!string.IsNullOrWhiteSpace(_moudleResult))
            {

                resultModel.ResultType = "1";
                resultModel.ResultData = _moudleResult;
                resultModel.AdditionalData = LdtJudge.DataEngine.Common.DataSave.DataResultSave.AnalysisRequests; ;
            }
            return resultModel;
        }

    }
}
